// components/play-toolbar/index.js
import {playerStore} from '../../store/index'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    
  },

  /**
   * 组件的初始数据
   */
  data: {
    playListSongs: [],   // 播放的歌单
    playListIndex: 0,    // 歌曲所在歌单的位置
    currentSong:{},     // 当前播放的歌曲
    isPlaying:false,  // 播放/暂停
    isShowPlayToolbar: false, // 没有内容隐藏工具栏
  },
  // 生命周期
  lifetimes:{
    attached(){
      playerStore.onStates(["playListSongs", "playListIndex","isPlaying"], ({ playListSongs, playListIndex,isPlaying }) => {
        if(playListSongs){
          this.setData({playListSongs})
        }
        if(playListIndex != undefined){
          this.setData({playListIndex})
        }
        if (isPlaying != undefined) {
          this.setData({isPlaying})
        }
        this.setData({currentSong:this.data.playListSongs[this.data.playListIndex]})
       console.log(this.data.currentSong);
        if (!this.data.currentSong) {
          this.setData({isShowPlayToolbar:false})
        }else{
          this.setData({isShowPlayToolbar:true})
        }
      })
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 播放/暂停
    handlePlayBtnClick() {
      playerStore.dispatch("changeMusicIsPlayingAction")
    },
    // 播放下一首
    handleNextSongClick(){
      playerStore.dispatch("changeNewMusicAction", "next")
    },
    handlePlayInfoClick(){
      wx.navigateTo({
        url: '../../pages/music-player/index?id=' + this.data.currentSong.id,
      })
    }
  }
})
